# python内置
import json
import xml.etree.ElementTree as ET
# 第三方库
import cssutils
# 自定义库
from configs.tools_configs import HANDLE_OPERATE_TYPES
from common.utils.message import fun_info


class CheckContent:
    def check(cls,check_type:str, content:str)->bool:
        if check_type in HANDLE_OPERATE_TYPES:
            print('检查格式',check_type)
            check_flag,r_content = eval('cls.check_'+check_type)(content)
            print('检查结果',check_flag)
            if check_flag:
                return fun_info(check_flag,r_content)
            return fun_info(False,f'{check_type} is illegal !')
        else:
            return fun_info(False,'not support unknown format type !')
    
    def check_JSON(cls,content):
        try:
            r_content = json.loads(json.dumps(content))
        except ValueError:
            return False,''
        return True,r_content

    def check_XML(cls,content):
        try:
            ET.fromstring(content)
            return True,''
        except ET.ParseError:
            return False,''

    def check_CSS(cls,content):
        try:
            cssutils.parseString(content)
            return True,''
        except ET.ParseError:
            return False,''

if __name__ == '__main__':
    HANDLE_OPERATE_TYPES = ['JSON','CSS','XML']
    CheckContent().check('JSON',str({'a':1}))